<?php
class Payment_IndexController extends Zendvn_Controller_Action{
	
	public  function init(){
		
		$this->createTemplate(APPLICATION_PATH . '/templates/form','template.ini','template');

		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if(!isset($data)){
			$this->_redirect("/registration/auth/login");
		}
		
	}
	
	public function indexAction(){
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
			
		
		if($data->packet == 1){

			$max_price = 300000;
			$max_day = 90;
			
		}else{
			
			$max_price = 600000;
			$max_day = 180;
			
		}
		
		$payment_transaction = new Payment_Model_Transaction();
		$user = new Registration_Model_Auth();
		
		if($this->getRequest()->isPost()){
			
			//Lấy thông tin giao dịch
			$transaction_info="";
			//Lấy mã đơn hàng 
			$order_code=@$_POST["order_code"];
			//Lấy email 
			$order_email="";
			//Lấy mobile 
			$order_mobile="";
			//Lấy card_type 
			$card_type=@$_POST["card_type"];
			//Lấy card_code 
			$card_code=@$_POST["card_code"];
			//Lấy card_seri 
			$card_seri=@$_POST["card_seri"];
		
			//Khai báo đối tượng của lớp PG_Checkout
			$shoha= new Shoha_Shoha();			
			$arrResult = $shoha->doChargeCard($card_code, $card_type, $transaction_info, $order_code, $order_email, $order_mobile, $card_seri);
			
			//Insert Transaction
			
			
			
			$arr = array(
				"userid"=>$data->id,
				"payment_type"=>$card_type,
				"payment_status"=>"nạp",
				"credit"=>$arrResult["price"],
				"debit"=>0,
				"card_code"=>$card_code,
				"card_seri"=>$card_seri,
				"order_code"=>$order_code,			
			);
			
			$payment_transaction->insertTransaction($arr);
			
			$this->view->result = $arr;
			
			//Count Price User
				
			$count_price_add = 0;
			
			$list = $payment_transaction->getBalanceDateExpire($data->id);
			
			foreach( $list as $key => $val){
				$count_price_add += (int)$val["credit"];
			}

			$price_need_add = $max_price - $count_price_add;
			
			$this->view->price_need_add = $price_need_add; 
			
			//Update Expire Date
			
			if($count_price_add != 0 && $count_price_add % $max_price == 0){
				
				if($max_price == 300000){
					$days = (int)($count_price_add/$max_price)*$max_day; 	
				}else{
					$days = (int)($count_price_add/$max_price)*$max_day + 30;  // add 1 tháng khuyen mai
				}
				
				$user_item = $user->getItemById($data->id); 
				
				if($user_item["expire_date"] == 0){                        // check expire null, get time now
					$user_item["expire_date"]  = date('Y-m-d h:i:s',time());
				}
				
				$expire_date_next = date('Y-m-d', strtotime($user_item["expire_date"]. ' + '.$days.' days'));
				
				$arr = array(
					"id"=>$data->id,
					"expire_date"=>$expire_date_next,
				);
				
				$user->updateProfile($arr);

				$data->expire_date = $expire_date_next;
				
				
			}
			
		}
		
		$this->view->user_item = $user->getItemById($data->id);
			
	}
	
}